<?php

class Admin_Table_Cities extends Apk_Db_Table
{
	protected $_name      = 'cities';
    protected $_primary   = 'id';
    protected $_ERROR = '';
    protected $_lang;
    
    public function __construct($lang = 'ru')
    {
    	$this->_lang = $lang;
    	parent::__construct();
    }

    public function GetError()
    {
    	return $this->_ERROR;
    }
    
    public function GetDataList($page = 1, $sort=null, $page_count=null)
    {
    	$sel = $this->getAdapter()->select()
    		  ->from($this->_name, array('id', 'name' => 'name_'.$this->_lang))
    		  ->joinLeft('countries', 'countries.id = cities.id_country', array('country' => 'name_'.$this->_lang))
    		  ->joinLeft('regions', 'regions.id = cities.id_region', array('region' => 'name_'.$this->_lang));    		  
    		  
		if(!is_null($sort) && strlen($sort) > 0) 
		{			
			$sel->order($sort);			
		}
		else $sel->order($this->_name.'.name_'.$this->_lang);  		
		

    	return $this->setDecorator('simpleDecorator')->getPager($sel, $page, array('perPage' => $page_count));
    } 
    
    public function GetDataRow($id)
    {
    	$sel = $this->getAdapter()->select()
    				->from($this->_name)
    				->where('id = ?', $id);

    	return $this->setDecorator('simpleDecorator')->getList($sel)->getRow(0)->toArray();
    }

    
    public function GetCountriesList($a = null)
    {
    	$ret = array();

		$sel = $this->getAdapter()->select()
					->from('countries', array('id', 'name' => 'name_'.$this->_lang))
					->order('name_'.$this->_lang);		
					
		$data = $this->getList($sel)->toArray();

    	if(count($data) > 0 && !is_null($a))
    	foreach($data as $k => $v)
    	  	if($v['id'] == $a) $data[$k]['__attributes']['active'] = 1;

    	return $data;
    }    

    public function GetCitiesList($a = null, $country_id = null, $region_id = null)
    {
    	$ret = array();

		$sel = $this->getAdapter()->select()
					->from('cities', array('id', 'name' => 'name_'.$this->_lang))
					->order('name_'.$this->_lang);
					
		if(!is_null($country_id)) $sel->where('id_country = ?', $country_id);					
		if(!is_null($region_id)) $sel->where('id_region = ?', $region_id);				
		
					
		$data = $this->getList($sel)->toArray();

    	if(count($data) > 0 && !is_null($a))
    	foreach($data as $k => $v)
    	  	if($v['id'] == $a) $data[$k]['__attributes']['active'] = 1;

    	return $data;
    }

    public function GetRegionsList($a = null, $country_id=null)
    {
    	$ret = array();   

		$sel = $this->getAdapter()->select()
				->from('regions', array('id', 'name' => 'name_'.$this->_lang))
				->order('name_'.$this->_lang);
				
		if(!is_null($country_id)) $sel->where('id_country = ?', $country_id);
				
		$data = $this->getList($sel)->toArray();
		
    	if(count($data) > 0 && !is_null($a))
    	foreach($data as $k => $v)
    	  	if($v['id'] == $a) $data[$k]['__attributes']['active'] = 1;

    	return $data;
    }
        


	public function insert(array $data)
    {

        $data['name_ru'] = trim($data['name_ru']);
        if($data['name_ru'] == ''){
        	$this->_ERROR = 'Необходимо ввести Название';
        	return false;
        }

        parent::insert($data);

		return true;
    }

    public function update(array $data, $id)
    {
        $data['name_ru'] = trim($data['name_ru']);
        if($data['name_ru'] == ''){
        	$this->_ERROR = 'Необходимо ввести Название';
        	return false;
        }

		return parent::update($data, 'id = '.$id);
    }

    public function remove($items)
    {
    	foreach($items as $k => $id)
    	{	    	
	    	parent::delete('id = '.$id);
    	}    	
    	return true;
	}


    /********************
     ***  Decorators  ***
    *********************/

    public function simpleDecorator($key, $value)
    {
        switch ($key) {
            case 'public_date':
            	$result[$key]['__attributes'] = $this->timestampToArray($value);
            	break;
 			case 'contacts':
            case 'text':
            	$result[$key] = stripslashes($value);
            	break;
            default:
                $result[$key] = $value;
                break;
        }
        return $result;
    }

}
?>